//
//  Utility.m
//  Broker
//
//  Created by 阮琦 on 16/2/17.
//  Copyright © 2016年 FMM. All rights reserved.
//

#import "Utility.h"

NSString* storeFile(NSString* key)
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *filePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:[key stringByAppendingString:@".archive"]];
    
    //NSLog(@" Cache toFile %@ ",filePath);
    
    return filePath;
}



@implementation Utility

+ (void)setLabel:(UILabel *)label withFonts:(UIFont *)textFont textColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment {
        label.font = textFont;
        label.textColor = textColor;
        label.textAlignment = textAlignment;
}

+ (void)setLabelArray:(NSArray *)labels withFonts:(UIFont *)textFont textColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment {
        for (id item in labels) {
            if ([item isKindOfClass:[UILabel class]]) {
                UILabel *label = item;
                label.font = textFont;
                label.textColor = textColor;
                label.textAlignment = textAlignment;
            }
        }
}

+(BOOL)setObject:(id)data key:(NSString*) key {
    return [NSKeyedArchiver archiveRootObject:data toFile:storeFile(key)];
}

+(id) getObjectForKey:(NSString*) key {
    return [NSKeyedUnarchiver unarchiveObjectWithFile:storeFile(key)];
}

+(BOOL)clearObjectForKey:(NSString*) key {
    NSString* filePath = storeFile(key);
    
    NSFileManager* fileManager=[NSFileManager defaultManager];
    [fileManager removeItemAtPath:filePath error:nil];
    
    if( ![fileManager fileExistsAtPath:filePath])
    {
        return YES;
    }
    return NO;
}

@end
